<?php

namespace Mlym\CodeGeneration\InitGeneration\Controller\Method;

use App\Logic\Authorization;
use Mlym\CodeGeneration\ClassGeneration\MethodAbstract;

class OnRequest extends MethodAbstract
{

    function addMethodBody()
    {
        $this->classGeneration->getPhpNamespace()->addUse('App\Exception\AuthorizationException');
        $this->classGeneration->getPhpNamespace()->addUse('App\Logic\Authorization');

        $this->method->addParameter('action')->setType('string')->setNullable();
        $this->method->setReturnType('bool');
        $this->method->addBody(<<<BODY
/**
 * Content-Type 为Application/json 获取请求内容
 */
\$requestBodyRaw = \$this->request()->getBody()->__toString();
\$requestBodyRaw && \$this->request()->withParsedBody(json_decode(\$requestBodyRaw, true));

/**
 * 验证 Jwt Token 及 权限
 */
Authorization::getInstance()->verify(\$this->request());
return true;
BODY
        );
    }

    function getMethodName(): string
    {
        return 'onRequest';
    }

    public function addComment()
    {
        $this->method->addComment(<<<BODY
@param string|null \$action
@return bool|null
@throws AuthorizationException
BODY
        );
    }
}